<?php
/**
* alltosun.com 焦点图控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-8 下午3:56:59 $
* $Id$
*/

class Action
{
    private $per_page = 20;
    /**
     * 显示焦点图列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();
        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%'.$search_filter['title'].'%';
        }
        if (isset($search_filter['res_name']) && $search_filter['res_name']) {
            $filter['res_name'] = $search_filter['res_name'];
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        $filter['status'] = $status;

        $focus_list = array();
        $focus_count = _model('focus')->getTotal($filter);
        if ($focus_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($focus_count)) {
                Response::assign('pager', $pager);
            }

            $focus_list = _model('focus')->getList($filter, ' ORDER BY `view_order` ASC '.$pager->getLimit($page));
        }

        Response::assign('focus_list', $focus_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);

        Response::display('admin/focus_list.html');
    }

    /**
     * 添加焦点图
     */
    public function add()
    {
        Response::display('admin/add.html');
    }

    /**
     * 编辑焦点图
     */
    public function edit()
    {
        $id = Request::getParam('id', 0);
        $focus_info = _uri('focus', $id);

        Response::assign('focus_info', $focus_info);
        Response::assign('id', $id);

        Response::display('admin/add.html');
    }

    /**
     * 保存
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        $id   = Request::getParam('id', 0);

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = trim(AnFilter::filter_string($v));
            }
        }

        if (!isset($info['title']) || !$info['title']) {
            return '请填写焦点图标题';
        }

        // 上传图片
        if ($_FILES['cover']['name']) {
            try {
                $return_data = upload_file($_FILES['cover'], false);
                $info['cover'] = $return_data['file_path'];
            } catch (Exception $e) {
                return $e->getMessage();
            }
        }

        if ($id) {
            $focus_info = _uri('focus', $id);
            if (!$focus_info) {
                return '该内容不存在';
            }

            _model('focus')->update($id, $info);
        } else {
            if (!$info['cover']) {
                return '请上传焦点图';
            }
            _model('focus')->create($info);
        }

        Response::redirect(AnUrl('focus/admin'));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $focus_info = _uri('focus', (int)$v);
            if (!$focus_info) {
                continue;
            }

            if (1 == $focus_info['status']) {
                _model('focus')->update($v, array('status'=>0));
            } else {
                _model('focus')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $focus_info = _uri('focus', $id);
        if ($status == $focus_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('focus')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    /**
     * 显示子版块列表
     * @return array
     */
    public function check_columns()
    {
        $position = Request::getParam('position', 0);
        if (!$position) {
            return '请选择要添加子板块的位置';
        }

        $focus_columns_list = _model('focus_columns')->getList(array('position'=>$position));

        return array('info'=>'ok', 'columns_list'=>$focus_columns_list);
    }

    /**
     * 更新子版块
     * @return array
     */
    public function update_columns()
    {
        $position = Request::getParam('position', 0);
        $title    = Request::getParam('title', '');
        $id       = Request::getParam('id', 0);

        if (!$position) {
            return '请选择要添加子板块的位置';
        }

        if (!$title) {
            return '请填写子版块标题';
        }

        if ($id) {
            _model('focus_columns')->update($id, array('position'=>$position, 'title'=>$title));
        } else {
           _model('focus_columns')->create(array('position'=>$position, 'title'=>$title));
        }

        $focus_columns_list = _model('focus_columns')->getList(array('position'=>$position));

        return array('info'=>'ok', 'columns_list'=>$focus_columns_list);
    }

    /**
     * 删除子版块
     * @return array
     */
    public function delete_columns()
    {
        $id = Request::getParam('id', 0);
        if (!$id) {
            return '请选择要删除的子板块';
        }

        _model('focus_columns')->delete($id);

        return 'ok';
    }
}
?>